package artid.com.arttogo.activity;
//艺术品详情滑动的承载页
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.CollectDetFragmentAdapter;
import artid.com.arttogo.bean.ShareItem;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.utils.Config;
import artid.com.arttogo.utils.NetUtils;
import artid.com.arttogo.view.SubViewPager;

public class CollectionDetailPagerActivity extends BaseActivity implements OnClickListener {

	public static SubViewPager mViewPager;
	CollectDetFragmentAdapter adapter;
	ImageView ivShare;
	ShareItem shareitem;
	public static Boolean isCreat = true;
	public static TextView mTittle;
//	public static List<ShareItem> listshare = new ArrayList<ShareItem>();
	private String id="";//艺术品ID
	private String imgwide;//请求的图片宽度
	private String messid="";//消息ID
	ArrayList<String> arrayids;
	private String titileName;//标题名称
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		isCreat = true;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mContext = CollectionDetailPagerActivity.this;
		imgwide=(Integer.parseInt(NetUtils.getDisplayMetrics((Activity)mContext).get(NetUtils.DM_WIDTH)))+"";
		setContentView(R.layout.activity_collectdet_pager);
		mViewPager = (SubViewPager)findViewById(R.id.viewPager);
		mTittle = (TextView)findViewById(R.id.tittle_text);
		mTittle.setText("艺术品详情");
		mBack = (ImageView)findViewById(R.id.back_btn);
		mBack.setOnClickListener(this);
		ivShare = (ImageView)findViewById(R.id.share_collection_detail);
		ivShare.setVisibility(View.VISIBLE);
		ivShare.setOnClickListener(this);
		titileName = getIntent().getExtras().getString("titleName");
		adapter = new CollectDetFragmentAdapter(getSupportFragmentManager(),titileName);
//		String json = getIntent().getExtras().getString("id");
		id = getIntent().getExtras().getString("id");
		if (!TextUtils.isEmpty(id)) {
			arrayids = new ArrayList<String>();
			arrayids.add(id);
		}else {
			 arrayids = getIntent().getExtras().getStringArrayList("id");
		}
		
		//判断是否来自机构
		Boolean isFromOrg = getIntent().getExtras().getBoolean("isOrg");
		if (isFromOrg==null) {
			isFromOrg = false;
		}
//		listshare = new ArrayList<ShareItem>();
		if (isFromOrg) {
			adapter.setCodeMode(isFromOrg);

			adapter.setInsData(arrayids);
			
//			for (int i = 0; i < arrayids.size(); i++) {
////				listshare.add(new ShareItem());
//			}
		}else{
			
			adapter.setData(arrayids);
			adapter.notifyDataSetChanged();
//			for (int i = 0; i < arrayids.size(); i++) {
////				listshare.add(new ShareItem());
//			}
		}
		index = 0;
		index = getIntent().getExtras().getInt("index");
		mViewPager.setAdapter(adapter);
		mViewPager.setOffscreenPageLimit(1);
		mViewPager.setOnPageChangeListener(mOnPageChangeListener);
		mViewPager.setCurrentItem(index);
//		getData();
	}
	
	public static int index = 0;
	private OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {
		@Override
		public void onPageSelected(int position) {
			index = position;
			mTittle.setText("艺术品详情");
//			if (!(listshare.size()>index)) {
//				return;
//			}
//			if (listshare.get(position)!=null) {
//				mTittle.setText(listshare.get(position).getTitle());
//			}
			
			
		} 

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			
		}
	};

	protected void onDestroy() {
		super.onDestroy();
//		listshare = new ArrayList<ShareItem>();
		
	};
	
	
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.back_btn:
			finish();
			break;
		case R.id.share_collection_detail:
			
//			if (!(listshare.size()>index)) {
//				return;
//			}
//			shareitem =  listshare.get(index);
			if (shareitem == null) {
				Config.showTip(CollectionDetailPagerActivity.this, "请检查网络连接");
			}
//			AppUtil.showShare(mContext, shareitem.getTitle(), shareitem.getContent(),
//					shareitem.getImageUrl(), shareitem.getUrl(),shareitem.getUid() , 
//					shareitem.getHeadUrl(), shareitem.getContent());
			AppUtil.shareWithTalk(mContext, shareitem.getTitle(), shareitem.getContent(), shareitem.getImageUrl(), shareitem.getUrl(), shareitem.getUid(), shareitem.getHeadUrl(),shareitem.getAname());
			break;
		default:
			break;
		}
	}
	
//	private void getData(){
//		VolleyBasic.getInstance(this).post(Cache.Newart_artDetails, 
//				VolleyMap.getInstance().Newart_artDetails(id,imgwide,messid), 
//				CollectionDetailPagerParser.class, new VolleyCallback() {
//
//			@Override
//			public void onSuccess(VolleyData vd) {
//				// TODO Auto-generated method stub
//				CollectionDetailPagerParser mBean=(CollectionDetailPagerParser) vd.getData();
////				Log.d("log", "作品详情："+mBean.toString());
//			}
//
//			@Override
//			public void onError(VolleyError volleyError) {
//				// TODO Auto-generated method stub
//			}
//		});
//	}
}
